//
//  BudgetOverview.swift
//  KoiAccounting
//  预算总览
//
//  Created by 纪洪文 on 15.04.25.
//

import SwiftUI

struct BudgetOverviewView: View {
    
    @Binding var totalBudgetAmount: Double
    @Binding var expenseAmount: Double
    @Binding var usePercent: Double
    @Binding var surplusAmount: Double
    
    var body: some View {
        VStack {
            if totalBudgetAmount == 0 {
                // 设置月预算
                HStack (spacing: 0) {
                    Text("设置月预算")
                        .font(.system(size: 14))
                        .fontWeight(.semibold)
                        .foregroundColor(Color.koiColorTitle)
                    
                    Spacer()
                    
                    Text(totalBudgetAmount.toAmountWithSign())
                        .font(.system(size: 14))
                        .fontWeight(.medium)
                        .foregroundColor(Color.koiColorTitleSecond)
                    Image("koi_ic_pencil")
                        .renderingMode(.template)
                        .resizable()
                        .frame(width: 14, height: 14)
                        .foregroundColor(Color.koiColorTitleSecond)
                        .padding(.leading, 5)
                }
            } else {
                HStack {
                    VStack (spacing: 6){
                        Text("月预算")
                            .font(.system(size: 14))
                            .fontWeight(.semibold)
                            .foregroundColor(Color.koiColorTitleSecond)
                        Text(totalBudgetAmount.toAmountWithSign())
                            .font(.system(size: 14))
                            .foregroundColor(Color.koiColorTitleSecond)
                    }
                    
                    Spacer()
                    
                    if surplusAmount < 0 {
                        VStack (alignment: .trailing) {
                            HStack (spacing: 0){
                                Text("已用:")
                                    .font(.system(size: 12))
                                    .foregroundColor(Color.koiColorPrimary)
                                Text(usePercent.toPercent())
                                    .font(.system(size: 12))
                                    .foregroundColor(Color.koiColorPrimary)
                                Text(" | 超支:")
                                    .font(.system(size: 12))
                                    .foregroundColor(Color.koiColorPrimary)
                                Text(abs(surplusAmount).toStringWithoutTrailingZeros())
                                    .font(.system(size: 12))
                                    .foregroundColor(Color.koiColorPrimary)
                            }
                            ProgressView(value: usePercent)
                                .accentColor(Color.koiColorPrimary)
                                .progressViewStyle(KoiLinearProgressViewStyle())
                                .frame(height: 6)
                        }
                        .padding(.leading, 15)
                    } else {
                        VStack (alignment: .trailing) {
                            HStack (spacing: 0){
                                Text("已用:")
                                    .font(.system(size: 12))
                                    .foregroundColor(Color.koiColorTitleSecond)
                                Text(usePercent.toPercent())
                                    .font(.system(size: 12))
                                    .foregroundColor(Color.koiColorTitleSecond)
                                Text(" | 剩余:")
                                    .font(.system(size: 12))
                                    .foregroundColor(Color.koiColorTitleSecond)
                                Text(surplusAmount.toStringWithoutTrailingZeros())
                                    .font(.system(size: 12))
                                    .foregroundColor(Color.koiColorTitleSecond)
                            }
                            ProgressView(value: usePercent)
                                .accentColor(Color.koiColorPrimary)
                                .progressViewStyle(KoiLinearProgressViewStyle())
                                .frame(height: 6)
                        }
                        .padding(.leading, 15)
                    }
                }
            }
        }
        .padding(EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16))
        .background(Color.koiColorBackgroundItem)
        .cornerRadius(16)
    }

}

@available(iOS 18, *)
#Preview {
    @Previewable @State var totalBudgetAmount: Double = 3000
    @Previewable @State var expenseAmount: Double = 2000
    @Previewable @State var usePercent = 0.3
    @Previewable @State var surplusAmount: Double = 1000
    BudgetOverviewView(
        totalBudgetAmount: $totalBudgetAmount,
        expenseAmount: $expenseAmount,
        usePercent: $usePercent,
        surplusAmount: $surplusAmount
    )
}
